<?php
namespace app\admin\controller;

use app\admin\model\Log as LogModel;

class Log extends Base
{
    /**
     * 显示资源列表
     *
     * @return \think\Response
     */
    public function index($search = '', $val = '', $page = 1, $limit = '')
    {

        $search = !empty($search) ? trim($search) : '';
        $val    = !empty($val) ? trim($val) : '';

        //静态分页方法
        // $where  = '';
        // $search = !empty($search) ? trim($search) : '';
        // $val    = !empty($val) ? trim($val) : '';
        // $limit  = !empty($limit) ? trim($limit) : config('webconfig.page');

        // if ($search == 'user' && $val) {
        //     $userid  = \Db::name('user')->where([['user_name', '=', $val]])->value('id');
        //     $where[] = ['user_id', '=', $userid];
        // } else if ($search == 'ip' && $val) {
        //     $where[] = ['ip', '=', $val];
        // }
        // $page = !empty($page) ? $page : 1;

        //$list  = LogModel::where($where)->paginate($limit, false, ['query' => Request::param()]); //paginate(10,false,['query'=>Request::param()])

        $this->assign([
            'search' => $search,
            'val'    => $val,
            //'xls'    => $this->xls($list),
        ]);
        return $this->fetch();
    }

    public function ajaxlist($search = '', $val = '', $page = 1, $limit = '')
    {
        $where  = '';
        $search = !empty($search) ? trim($search) : '';
        $val    = !empty($val) ? trim($val) : '';
        $limit  = !empty($limit) ? trim($limit) : config('webconfig.page');

        if ($search == 'user' && $val) {
            $userid  = \Db::name('user')->where([['user_name', '=', $val]])->value('id');
            $where[] = ['user_id', '=', $userid];
        } else if ($search == 'ip' && $val) {
            $where[] = ['ip', '=', $val];
        }
        $page = !empty($page) ? $page : 1;

        //$list  = LogModel::where($where)->paginate($limit, false, ['query' => Request::param()]); //paginate(10,false,['query'=>Request::param()])
        $list  = LogModel::where($where)->page($page, $limit)->order('id', 'asc')->select();
        $count = LogModel::where($where)->count();
        foreach ($list as $k => $v) {
            $list[$k]['title'] = $v->rulename->title;
            $list[$k]['user']  = $v->username->user_name;
            $list[$k]['text']  = $v['url'] . '--' . $v['operation_id'] . '--' . $v['text'];

        }
        //print_r($list);exit
        $list = ["code" => 0, "msg" => "加载中", "count" => $count, "data" => $list];
        return json($list);
    }

    /**
     * 删除指定资源
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function deleteAll($ids = '')
    {
        $ids  = !empty($ids) ? $ids : '';
        $data = LogModel::destroy($ids);
        if (!empty($data)) {
            return $this->success('批量删去成功', 'index');
        }
    }

    public function xls($search = '', $val = '', $limits = 50, $file = '', $tabname = '记录表')
    {

        $where = '';

        if ($search == 'user' && $val) {
            $userid  = \Db::name('user')->where([['user_name', '=', $val]])->value('id');
            $where[] = ['user_id', '=', $userid];
        } else if ($search == 'ip' && $val) {
            $where[] = ['ip', '=', $val];
        }
        $count = LogModel::where($where)->count();
        //echo ceil($count / $limit);
        $page = (int) (ceil($count / $limits) + 1);
        //echo $page;

        if (\Request::has('down', 'get')) {

            $objPHPExcel = new \PHPExcel();
            //$filename    = 'think_admin.xls';
            $file        = !empty($file) ? $file : time();
            $file        = $file . '.xls';
            $objPHPExcel = new \PHPExcel();
            for ($i = 1; $i < $page; $i++) {
                if ($i > 1) {
                    $objPHPExcel->createSheet();
                }

                $list = LogModel::where($where)->order('id', 'asc')->page($i, $limits)->select();
                foreach ($list as $k => $v) {
                    $list[$k]['title'] = $v->rulename->title;
                    $list[$k]['user']  = $v->username->user_name;
                    $list[$k]['text']  = $v['url'] . '--' . $v['operation_id'] . '--' . $v['text'];
                }

                $objPHPExcel->setActiveSheetIndex($i - 1); //把当前创建的sheet设置为活动sheet
                $objSheet = $objPHPExcel->getActiveSheet(); //获得当前活动Sheet
                $objSheet->setTitle($tabname . $i);

                $objSheet->setCellValue('A1', 'id')
                    ->setCellValue('B1', '用户')
                    ->setCellValue('C1', '操作')
                    ->setCellValue('D1', '时间')
                    ->setCellValue('E1', 'url')
                    ->setCellValue('F1', '说明');

                $j = 2;
                foreach ($list as $v) {
                    $objSheet->setCellValue('A' . $j, $v['id'])
                        ->setCellValue('B' . $j, $v['user'])
                        ->setCellValue('C' . $j, $v['title'])
                        ->setCellValue('D' . $j, $v['create_time'])
                        ->setCellValue('E' . $j, $v['url'])
                        ->setCellValue('F' . $j, $v['text']);

                    $j++;
                }
            }
            $objWriter = \PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
            header('Content-Type: application/vnd.ms-excel'); //告诉浏览器将要输出excel03文件
            header('Content-Disposition: attachment;filename="' . $file . '"'); //告诉浏览器将输出文件的名称(文件下载)
            header('Cache-Control: max-age=0'); //禁止缓存
            $objWriter->save("php://output");
        } else {
            return $this->fetch();
        }
    }
}
